package com.mingmen.supplier.activity;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.imagepicker.ImagePicker;
import com.mingmen.supplier.R;
import com.mingmen.supplier.adapter.CommodityManagerAdapter;
import com.mingmen.supplier.base.BaseBean;
import com.mingmen.supplier.http.callback.StringCallback;
import com.mingmen.supplier.httpconfig.HttpResponse;
import com.mingmen.supplier.immersionbar.ImmersionBar;
import com.mingmen.supplier.model.CommodityShopModel;
import com.mingmen.supplier.model.RestaurantModel;
import com.mingmen.supplier.refresh.ClassicsFooter;
import com.mingmen.supplier.refresh.ClassicsHeader;
import com.mingmen.supplier.util.GsonUtil;
import com.mingmen.supplier.util.MyStringUtils;
import com.mingmen.supplier.util.SharePreferenceUtil;
import com.mingmen.supplier.util.ToolList;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class RestaurantHistoryActivity extends AppCompatActivity implements RestaurantHistoryAdapter.OnItemClickListener {

    //返回按钮
    @BindView(R.id.text_return)
    TextView textReturn;

    @BindView(R.id.recycle_shopo)
    RecyclerView mRecyclerView;

    RestaurantHistoryAdapter commodityAdapter;


    LinearLayoutManager linearLayoutManager;

    private List<RestaurantModel.DataBean> mListData = new ArrayList<>();

    String token;

    @BindView(R.id.linear_return)
    LinearLayout linear_return;
    String restaurantId;
    String flag;
    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    int pageNumber = 1;
    int totalPage;
    @BindView(R.id.layout_alldata)
    LinearLayout layout_alldata;
    @BindView(R.id.layout_nodata)
    LinearLayout layout_nodata;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_restauranthistory);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f)
                .init();
        flag = getIntent().getStringExtra("flag");
        token = (String) SharePreferenceUtil.get(this, "token", "");
        restaurantId = getIntent().getStringExtra("restaurantId");
        linearLayoutManager = new LinearLayoutManager(this);

        mRecyclerView.setLayoutManager(linearLayoutManager);
        commodityAdapter = new RestaurantHistoryAdapter(this, mListData, flag);
        mRecyclerView.setAdapter(commodityAdapter);
        commodityAdapter.setOnItemClickListener(this);
        getData(pageNumber);
        refreshLayout.setRefreshHeader(new ClassicsHeader(RestaurantHistoryActivity.this));
        refreshLayout.setRefreshFooter(new ClassicsFooter(RestaurantHistoryActivity.this));

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                if (mListData.size() > 0) {
                    mListData.clear();
                }
                pageNumber = 1;

//                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                getData(pageNumber);
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                if (pageNumber >= totalPage) {
                    Toast.makeText(RestaurantHistoryActivity.this, "已到最后一页", Toast.LENGTH_SHORT).show();
                    refreshlayout.finishLoadMore(false);//传入false表示加载失败
                } else {
                    pageNumber++;

                    getData(pageNumber);

                    refreshlayout.finishLoadMore(100/*,false*/);//传入false表示加载失败
                }


            }
        });

    }

    public void getData(int pageNumber) {
        HttpResponse.getCooperationLog(token, restaurantId, pageNumber, new StringCallback(this) {
            @Override
            public void onSuccess(String s, Call call, Response response) {

                RestaurantModel restaurantModel = new RestaurantModel();
                restaurantModel = GsonUtil.GsonToBean(s, RestaurantModel.class);
                mListData.addAll(restaurantModel.getData());
                totalPage = restaurantModel.getTotalPage();
                if (mListData.size() == 0) {
                    layout_alldata.setVisibility(View.GONE);
                    layout_nodata.setVisibility(View.VISIBLE);
                }else {
                    layout_alldata.setVisibility(View.VISIBLE);
                    layout_nodata.setVisibility(View.GONE);
                }
                commodityAdapter.setDataList(mListData);
            }

            @Override
            public void onAfter(String s, Exception e) {
                super.onAfter(s, e);
                refreshLayout.finishRefresh(100/*,false*/);//传入false表示刷新失败
                refreshLayout.finishLoadMore(100/*,false*/);//传入false表示加载失败
            }
        });
    }

    @OnClick({R.id.text_return, R.id.linear_return})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.text_return:
            case R.id.linear_return:
                finish();
                break;


        }
    }


    @Override
    public void onItemClickListener(View view, int pos, List<RestaurantModel.DataBean> myLiveList) {

    }
}




